import time
import json


import MySQL
import Config as C


def createSample():
    # for i in range(10, 20):
    #     sql = 'CREATE TABLE lineitem_sample_200w_1w_' + str(i) + ' LIKE lineitem'
    #     MySQL.execute(sql)
    for i in range(10, 20):
        sql = 'INSERT INTO lineitem_sample_200w_1w_' + str(i) + ' SELECT * FROM lineitem_data_200w ORDER BY RAND() LIMIT 10000'
        MySQL.execute(sql)


def execute():
    tpchQueries = MySQL.query('SELECT * FROM tpch_query')
    for row in tpchQueries:
        query_id = row['id']
        base_sql = row['base_sql']
        query_type = row['query_type']
        related_table = row['related_table']
        if query_type == 'approximate':
            for k in range(20):
                table = related_table + '_' + '%02d' % k
                sql = base_sql.replace(related_table, table)
                start_time = int(round(time.time() * 1000))
                result = MySQL.query(sql)
                end_time = int(round(time.time() * 1000))
                execution_time = end_time - start_time
                MySQL.save('tpch_result', {
                    'query_id': query_id,
                    'table': table,
                    'result': json.dumps(result),
                    'execution_time': execution_time
                })
        elif query_type == 'accurate':
            for k in range(200, 510, 50):
                table = related_table + '_' + str(k) + 'w'
                sql = base_sql.replace(related_table, table)
                start_time = int(round(time.time() * 1000))
                result = MySQL.query(sql)
                end_time = int(round(time.time() * 1000))
                execution_time = end_time - start_time
                MySQL.save('tpch_result', {
                    'query_id': query_id,
                    'table': table,
                    'result': json.dumps(result),
                    'execution_time': execution_time
                })


def executeQuery(id):
    tpchQueries = MySQL.query('SELECT * FROM tpch_query WHERE id=' + str(id))
    for row in tpchQueries:
        query_id = row['id']
        base_sql = row['base_sql']
        query_type = row['query_type']
        related_table = row['related_table']
        if query_type == 'approximate':
            for k in range(100):
                table = related_table + '_sample_' + '%02d' % k
                sql = base_sql.replace(related_table, table)
                start_time = int(round(time.time() * 1000))
                result = MySQL.query(sql)
                end_time = int(round(time.time() * 1000))
                execution_time = end_time - start_time
                MySQL.save('tpch_result', {
                    'query_id': query_id,
                    'table': table,
                    'result': json.dumps(result),
                    'execution_time': execution_time,
                    'size': 1,
                    'normalized': 0
                })
        elif query_type == 'accurate':
            for k in range(C.DEFAULT_DATA_START, C.DEFAULT_DATA_END + C.DEFAULT_DATA_DELTA - 1,
                           C.DEFAULT_DATA_DELTA):
                table = related_table + '_inc_' + str(k) + 'w'
                sql = base_sql.replace(related_table, table)
                start_time = int(round(time.time() * 1000))
                result = MySQL.query(sql)
                end_time = int(round(time.time() * 1000))
                execution_time = end_time - start_time
                MySQL.save('tpch_result', {
                    'query_id': query_id,
                    'table': table,
                    'result': json.dumps(result),
                    'execution_time': execution_time,
                    'size': k,
                    'normalized': 0
                })


if __name__ == '__main__':
    # createSample()
    for i in range(19, 21):
        executeQuery(i)
